package midi;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;


public class MidiNote implements Trackable{
	
	private MidiSound midiSound;
	private long length;

	public MidiNote(int noteNumber, int velocity, long length){
		this.length = length;
		this.midiSound = new MidiSound(noteNumber, velocity);
	}
	
	public MidiNote(Pitch pitch, int velocity, long length){
		this.length = length;
		this.midiSound = new MidiSound(pitch, velocity);
	}
	
	public void setNoteNumber(int noteNumber){
		this.midiSound.setNoteNumber(noteNumber);
	}
	
	public int getNoteNumber(){
		return this.midiSound.getNoteNumber();
	}
	
	public void setVelocity(int velocity){
		this.midiSound.setVelocity(velocity);
	}
	
	public int getVelocity(){
		return this.midiSound.getVelocity();
	}
	
	public void setLength(long length){
		this.length = length;
	}
	
	public long getLength(){
		return length;
	}
	
	public void addToTrack(Track track, int channel, long prevTicks){
		try{
			ShortMessage endMsg = new ShortMessage();
			//endMsg.setMessage(ShortMessage.NOTE_OFF, channel, noteNumber, 127);
			
			//track.add(new MidiEvent(endMsg, prevTicks));
			
			ShortMessage msg = new ShortMessage();
			msg.setMessage(ShortMessage.NOTE_ON, channel, this.getNoteNumber(), this.getVelocity());

			track.add(new MidiEvent(msg, prevTicks));

			
			//endMsg = new ShortMessage();
			endMsg.setMessage(ShortMessage.NOTE_OFF, channel, this.getNoteNumber(), this.getVelocity());
			
			track.add(new MidiEvent(endMsg, prevTicks + length));
		}
		catch (Exception e){
		}		
	}
	
}
